//
//  ViewController4.m
//  测试
//
//  Created by 李李善 on 16/7/30.
//  Copyright © 2016年 李李善. All rights reserved.
//

#import "BaseViewController.h"
#import "NSObject+method.h"
@interface BaseViewController ()<UIGestureRecognizerDelegate>
@end

@implementation BaseViewController
- (void)dealloc
{
    
    NSLog(@"%@ 这个类被强暴了",NSStringFromClass([self class]));
    //移除通知
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}



- (void)viewDidLoad {//自定义视图。
    [super viewDidLoad];
    
    self.view.opaque = YES;
    
    self.view.backgroundColor = [UIColor whiteColor];
    

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        [self.navigationController.interactivePopGestureRecognizer setEnabled:YES];
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }

//    self.edgesForExtendedLayout = UIRectEdgeNone;
//    不让控制器自动生成64的偏移
//    self.automaticallyAdjustsScrollViewInsets = NO;
  
    
    
    
}
//-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
//{
//    if (self.childViewControllers.count==1) {
//        return NO;
//    }else
//    {
//        return YES;
//    }
//}


- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

@end









@interface BaseTableViewController ()<UIGestureRecognizerDelegate>

@end

@implementation BaseTableViewController

- (void)dealloc
{
    NSLog(@"%@ 这个类被强暴了",NSStringFromClass([self class]));
    //移除通知
    [[NSNotificationCenter defaultCenter]removeObserver:self];
 
}



//-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
//{
//    if (self.childViewControllers.count==1) {
//        return NO;
//    }else
//    {
//        return YES;
//    }
//}



- (void)viewDidLoad {//自定义视图。
    [super viewDidLoad];
    
    self.view.opaque = YES;
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        [self.navigationController.interactivePopGestureRecognizer setEnabled:YES];
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }

}

@end








@interface BaseCollectionViewController ()<UIGestureRecognizerDelegate>

@end
@implementation BaseCollectionViewController

- (void)dealloc
{
    NSLog(@"%@ 这个类被强暴了",NSStringFromClass([self class]));
    //移除通知
    [[NSNotificationCenter defaultCenter]removeObserver:self];

}

//-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
//{
//    if (self.childViewControllers.count==1) {
//        return NO;
//    }else
//    {
//        return YES;
//    }
//}

- (void)viewDidLoad {//自定义视图。
    
    [super viewDidLoad];
    
    self.view.opaque = YES;
    
    self.view.backgroundColor = [UIColor whiteColor];
   
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        [self.navigationController.interactivePopGestureRecognizer setEnabled:YES];
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
    
}



@end


@implementation UIViewController (rootVC)
+(UIViewController *)rootViewController
{
    return [UIApplication sharedApplication].keyWindow.rootViewController;
}

+(UIWindow *)appWindow
{
    return [UIApplication sharedApplication].keyWindow;
}
+(id)appDelegate
{
    return (id)[UIApplication sharedApplication].delegate;
}
@end


static char * block = "block";
static char * load = "isLoad";
@interface UIViewController()
@property(nonatomic,copy)void(^layoutBlock)(UIView *view,CGPoint origin,CGSize size,CGPoint center);
@property(nonatomic,assign)BOOL isLoad;
@end
@implementation UIViewController (onLoad)

-(CGFloat)navBarHeight
{
   CGRect r = self.navigationController.navigationBar.frame;
   return r.origin.y+r.size.height;
}

+(void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class obj = [self class];
        classSwizzleInstanceMethod(obj, @selector(viewWillLayoutSubviews), @selector(onViewWillLayoutSubviews));
        classSwizzleInstanceMethod(obj, @selector(viewDidLayoutSubviews), @selector(onViewDidLayoutSubviews));
        classSwizzleInstanceMethod(obj, @selector(viewWillAppear:), @selector(onViewWillAppear:));
    });
}
-(void)onViewWillAppear:(BOOL)animated
{
    if (self.isLoad==NO) {
        
        [self onLoadDataForPageNum:1];
        id view;
        if ([self isKindOfClass:[UICollectionViewController class]]) {
            UICollectionViewController *vc =(id)self;
            view = vc.collectionView;
        }else if ([self isKindOfClass:[UITableViewController class]])
        {
            UITableViewController *vc =(id)self;
            view = vc.tableView;
        }else
        {
            view = self.view;
        }
        [self onLoadViewByWillAppear:view];
    }
}
-(void)onViewWillLayoutSubviews
{
    if(self.layoutBlock)
    {
        CGRect frame = self.view.frame;
        UIView * view;
        if ([self isKindOfClass:[UICollectionViewController class]]) {
            UICollectionViewController *vc =(id)self;
            view = vc.collectionView;
        }else if ([self isKindOfClass:[UITableViewController class]])
        {
            UITableViewController *vc =(id)self;
            view = vc.tableView;
        }else
        {
            view = self.view;
        }
        self.layoutBlock(view,frame.origin,frame.size,view.center);
    }
}


- (void)onViewDidLayoutSubviews{
    if (NO==self.isLoad) {
        self.isLoad = YES;
        [self onLoadAnimatedByDidAppear:self.model];
    }
}


-(void)onMainQueueAddSub:(void (^)(UIView *, CGPoint, CGSize, CGPoint))layoutBlock
{
    self.layoutBlock = layoutBlock;
}

-(void)onLoadDataForPageNum:(NSInteger)pageNum
{
    NSLog(@"%ld",pageNum);
}
-(void)onLoadAnimatedByDidAppear:(id)model
{
    NSLog(@"%@",model);
}
-(void)onLoadViewByWillAppear:(UIView *)view
{
    NSLog(@"%@",view);
}
-(void (^)(UIView *, CGPoint, CGSize, CGPoint))layoutBlock
{
    return objc_getAssociatedObject(self,block);
}

- (void)setLayoutBlock:(void (^)(UIView *, CGPoint, CGSize, CGPoint))layoutBlock
{
    objc_setAssociatedObject(self, block,layoutBlock, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(BOOL)isLoad
{
    return [objc_getAssociatedObject(self,load) integerValue];
}
-(void)setIsLoad:(BOOL)isLoad
{
    objc_setAssociatedObject(self,load,@(isLoad), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
